#include "Headers/UI/clientchoosedialog.h"
#include "ui_clientchoosedialog.h"

#include "../../Headers/UI/clientsdialog.h"

ClientChooseDialog::ClientChooseDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ClientChooseDialog)
{
    ui->setupUi(this);

    this->setWindowFlags(Qt::Dialog | Qt::WindowTitleHint);

    currentClient = NULL;

    Update();
}

ClientChooseDialog::~ClientChooseDialog()
{
    delete ui;
}

Client * ClientChooseDialog::getCurrentClient()
{
    return currentClient;
}

void ClientChooseDialog::on_pushButton_3_clicked()
{
    ClientsDialog clientDialog;
    clientDialog.exec();
}

void ClientChooseDialog::Update()
{
    //заполняем таблицу клиентов
    ClientDataAccessor dataAccessor;
    QSqlTableModel * model = dataAccessor.getClients();
    model->setHeaderData(1, Qt::Horizontal, trUtf8("Имя"));
    model->setHeaderData(2, Qt::Horizontal, trUtf8("Фамилия"));
    model->setHeaderData(3, Qt::Horizontal, trUtf8("Документ"));
    model->setHeaderData(4, Qt::Horizontal, trUtf8("Телефон"));
    ui->tableView->setModel(model);
    ui->tableView->hideColumn(0);
    ui->tableView->hideColumn(4);
    ui->tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
}

void ClientChooseDialog::on_tableView_doubleClicked(const QModelIndex &index)
{
    int id = ui->tableView->model()->data(index).toInt();
    ClientDataAccessor dataAccessor;
    currentClient = dataAccessor.getClient(id);
    this->close();
}

void ClientChooseDialog::on_tableView_clicked(const QModelIndex &index)
{
    int row = index.row();
    int id = ui->tableView->model()->data(ui->tableView->model()->index(row, 0)).toInt();
    ClientDataAccessor dataAccessor;
    currentClient = dataAccessor.getClient(id);
    this->close();
}
